﻿using MaterialSkin.Controls;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPortT
{
    public partial class FormPLC : MetroForm
    {
        public FormPLC()
        {
            InitializeComponent();
        }

        private void frmIdCard_Load(object sender, EventArgs e)
        {
            try
            {
                string[] portNames = SerialPort.GetPortNames();
                for (int i = 0; i < portNames.Length; i++)
                {
                    string com = portNames[i];
                    this.comboBox1PortName.Items.Add(com);
                }
                comboBox1PortName.SelectedIndex = 0;

                #region 绘制成圆
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(this.label1.ClientRectangle);
                Region region = new Region(gp);
                this.pictureBox1.Region = region;
                this.pictureBox2.Region = region;
                this.pictureBox3.Region = region;
                this.pictureBox4.Region = region;
                this.pictureBox5.Region = region;
                this.pictureBox6.Region = region;
                this.pictureBox7.Region = region;
                this.pictureBox8.Region = region;

                label1.Region = region;
                label2.Region = region;
                label3.Region = region;
                label4.Region = region;
                label5.Region = region;
                label6.Region = region;
                label7.Region = region;
                label8.Region = region;
                gp.Dispose();
                region.Dispose();
                #endregion
            }
            catch
            {
               // MessageBox.Show("找不到通讯串口！", "串口提示");
            }
        }

        private void frmIdCard_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.serialPort1.IsOpen)
                {
                    this.closing = true;
                    while (this.listening)
                    {
                        Application.DoEvents();
                    }
                    this.serialPort1.Close();
                    this.closing = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("关闭串口失败！\r\n:" + ex.Message);
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!this.closing)
            {
                try
                {
                    this.listening = true;
                    Thread.Sleep(200);
                    if (this.serialPort1.IsOpen)
                    {
                        int receiveNumber = this.serialPort1.BytesToRead;
                        if (receiveNumber != 0)
                        {
                            string str = null;
                            byte[] bs = new byte[receiveNumber];
                            this.serialPort1.Read(bs, 0, receiveNumber);
                            str = ByteArrayToString(bs);
                            if (str.Length >= 13)
                            {
                                this.label1.Invoke(new MethodInvoker(delegate
                                {
                                    try
                                    {
                                        string dd = this.plcControl.GetChecks(str);
                                        if (this.plcControl.GetX0(str))
                                        {
                                            this.label1.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label1.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX1(str))
                                        {
                                            this.label2.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label2.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX2(str))
                                        {
                                            this.label3.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label3.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX3(str))
                                        {
                                            this.label4.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label4.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX4(str))
                                        {
                                            this.label5.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label5.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX5(str))
                                        {
                                            this.label6.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label6.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX6(str))
                                        {
                                            this.label7.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label7.BackColor = Color.White;
                                        }
                                        if (this.plcControl.GetX7(str))
                                        {
                                            this.label8.BackColor = Color.Red;
                                        }
                                        else
                                        {
                                            this.label8.BackColor = Color.White;
                                        }
                                    }
                                    catch
                                    {
                                    }
                                    this.plcControl.GetAllInCheck();
                                }));
                            }
                        }
                    }
                }
                finally
                {
                    this.listening = false;
                }
            }
        }

        public static string ByteArrayToString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            for (int i = 0; i < data.Length; i++)
            {
                byte b = data[i];
                sb.Append(Convert.ToChar(b));
            }
            return sb.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.serialPort1.IsOpen)
                {
                    this.closing = true;
                    while (this.listening)
                    {
                        Application.DoEvents();
                    }
                    this.serialPort1.Close();
                    this.closing = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("关闭串口失败！\r\n:" + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.comboBox1PortName.Text))
            {
                MessageBox.Show("请选择串口号！");
                this.comboBox1PortName.Focus();
            }
            else
            {
                string strStopBits = "1";
                this.serialPort1.PortName = this.comboBox1PortName.Text;
                this.serialPort1.BaudRate = Convert.ToInt32(cmbBaud.Text);
                this.serialPort1.DataBits = Convert.ToInt32(strDatabits.Text);
                this.serialPort1.Parity = Parity.Even;
                this.serialPort1.StopBits = (StopBits)int.Parse(strStopBits);
                this.serialPort1.Encoding = Encoding.ASCII;
                this.serialPort1.RtsEnable = true;
                try
                {
                    this.serialPort1.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开串口失败！\r\n:" + ex.Message);
                }
                this.plcControl = new DetaPLCControl(this.serialPort1);
                if (this.serialPort1.IsOpen)
                {
                    this.plcControl.GetAllInCheck();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                Thread.Sleep(100);
                if (this.pictureBox1.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY0ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY0OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox1.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox1.BackColor = Color.White;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox2.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY1ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY1OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox2.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox2.BackColor = Color.White;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox3.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY2ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY2OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox3.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox3.BackColor = Color.White;
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox4.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY3ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY3OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox4.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox4.BackColor = Color.White;
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox5.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY4ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY4OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox5.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox5.BackColor = Color.White;
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox6.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY5ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY5OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox6.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox6.BackColor = Color.White;
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox7.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY6ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY6OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox7.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox7.BackColor = Color.White;
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                if (this.pictureBox8.BackColor != Color.Red)
                {
                    this.sendFlg = true;
                    this.plcControl.SetY7ON();
                }
                else
                {
                    this.sendFlg = false;
                    this.plcControl.SetY7OFF();
                }
                if (this.sendFlg)
                {
                    this.pictureBox8.BackColor = Color.Red;
                }
                else
                {
                    this.pictureBox8.BackColor = Color.White;
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                this.plcControl.ClearAllOut();
                this.pictureBox1.BackColor = Color.White;
                this.pictureBox2.BackColor = Color.White;
                this.pictureBox3.BackColor = Color.White;
                this.pictureBox4.BackColor = Color.White;
                this.pictureBox5.BackColor = Color.White;
                this.pictureBox6.BackColor = Color.White;
                this.pictureBox7.BackColor = Color.White;
                this.pictureBox8.BackColor = Color.White;
            }
        }

        private static byte[] HexStrToHexByte(string hexStr)
        {
            byte[] returnByte = Encoding.Unicode.GetBytes(hexStr);
            return Encoding.ASCII.GetBytes(hexStr);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.plcControl.SetY5ON();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            this.plcControl.SetY3ON();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            this.plcControl.SetY2ON();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            this.plcControl.SetY4ON();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.plcControl.SetY0ON();
        }
    }
}
